package com.yulian.web_api.supplier_management;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;

import com.yulian.repository.CommonRepositoryImpl;
import com.yulian.util.CommonUtils;
import com.yulian.util.XLSX2CSV;

import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.Log4JLoggerFactory;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.pgclient.PgPool;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.Tuple;

public class HandleBatchSupplierInfo {

    final InternalLogger logger = Log4JLoggerFactory.getInstance(HandleBatchSupplierInfo.class);
    CommonRepositoryImpl commonRepository = new CommonRepositoryImpl();

    public Future<JsonObject> handleExcelFile(Vertx vertx, PgPool pool, JsonObject config, String file_name) {
        // 最后返回给客户端的 json 对象
        Promise<JsonObject> promise = Promise.promise();
        JsonObject responseJSObj = new JsonObject();
        responseJSObj.put("msg", "处理Excel文件结果:");

        this.commonRepository.getCon(pool)
        .onSuccess(conn -> {
            /*** 处理上传的excel
             * 利用OPCPackage 打开文件
             */
            int minColumns = -1;
            BufferedReader bReader;
            //2. read data from excel
            try (OPCPackage p = OPCPackage.open(file_name, PackageAccess.READ)) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                PrintStream out = new PrintStream(baos);
                //read the excel to csv
                XLSX2CSV xlsx2csv = new XLSX2CSV(p, out, minColumns);
                xlsx2csv.process();
                ByteArrayInputStream in = new ByteArrayInputStream(baos.toByteArray());
                bReader = new BufferedReader(new InputStreamReader(in));

                String line = "";
                //忽略前三行
                for (int i = 0; i < 3; i++) {
                    line = bReader.readLine();
                }
                //统计记录总数
                int count = 0;
                boolean item_not_null_valid = true;
                List<String> errMsgList = new ArrayList<String>();
                List<Tuple> supplier_info_batch = new ArrayList<>();

                @SuppressWarnings("rawtypes")
                List<Future> futures = new ArrayList<Future>();
                // 定义获取supplier_code的future (第一个)
                String supplier_code_sql = "select supplier_code from supplier_info order by id desc limit 1";
                Future<String> getSupplierCodeFuture = this.commonRepository.getCodeFuture(conn, supplier_code_sql, "get_supplier_code", "查询供应商编码 操作失败");
                futures.add(getSupplierCodeFuture);
                while ((line = bReader.readLine()) != null) {
                    item_not_null_valid = true;
                    if (line.trim() != "") {
                        count = count + 1;
                        String[] data = line.split(":");
                        // 获取给个字段的值
                        String goods_gather_area_name = "";
                        if (!data[0].toString().equals("")) {
                            goods_gather_area_name = data[0].toString();
                            //校验供应链商品聚集区名的合法性
                            String get_code_sql = "select distinct goods_gather_area_code from  goods_gather_area where goods_gather_area_name = $1";
                            Future<JsonObject> checkGoodsGatherAreaNameFuture = this.commonRepository.getCodeByNameFuture(conn, get_code_sql, goods_gather_area_name, "供应链商品聚集区名", count, "supplier_info_check_goods_gather_area_name");
                            futures.add(checkGoodsGatherAreaNameFuture);
                        } else {
                            errMsgList.add("第" + count + "行的供应链商品聚集区为空");
                            item_not_null_valid = false;
                        }
                        String class_lv1_name = "";
                        if (!data[1].toString().equals("")) {
                            class_lv1_name = data[1].toString();
                            //校验商品一级分类名的合法性
                            String get_code_sql = "select distinct class_code from goods_class where class_level = 2 and class_name = $1";
                            Future<JsonObject> checkClassLv1NameFuture = this.commonRepository.getCodeByNameFuture(conn, get_code_sql, class_lv1_name, "商品一级分类名", count, "supplier_info_check_class_lv1_name");
                            futures.add(checkClassLv1NameFuture);
                        } else {
                            errMsgList.add("第" + count + "行的商品一级分类为空");
                            item_not_null_valid = false;
                        }
                        String grade_name = "";
                        if (!data[2].toString().equals("")) {
                            grade_name = data[2].toString();
                            //校验供应商等级的合法性
                            String get_code_sql = "select distinct grade_code from supplier_grade where grade_name = $1";
                            Future<JsonObject> checkGradeNameFuture = this.commonRepository.getCodeByNameFuture(conn, get_code_sql, grade_name, "供应商等级名", count, "supplier_info_check_supplier_grade_name");
                            futures.add(checkGradeNameFuture);
                        } else {
                            errMsgList.add("第" + count + "行的供应商等级为空");
                            item_not_null_valid = false;
                        }

                        String supplier_name = "";
                        if (!data[3].toString().equals("")) {
                            supplier_name = data[3].toString();
                            //校验供应商名的合法性
                            String get_code_sql = "select distinct supplier_code from  supplier_info where supplier_name = $1";
                            Future<JsonObject> checkSupplierNameFuture = this.commonRepository.getCodeByNameFuture(conn, get_code_sql, supplier_name, "供应商名", count, "supplier_info_check_supplier_name");
                            futures.add(checkSupplierNameFuture);
                        } else {
                            errMsgList.add("第" + count + "行的供应商名为空");
                            item_not_null_valid = false;
                        }

                        String site_name = "";
                        if (!data[4].toString().equals("")) {
                            site_name = data[4].toString();//得到 无锡地区,常州地区
                            //校验数据的合法性
                            Future<JsonObject> checkSiteNameFuture = checkSiteNameFuture(conn, count, site_name);
                            futures.add(checkSiteNameFuture);
                        } else {
                            errMsgList.add("第" + count + "行的销售地区为空");
                            item_not_null_valid = false;
                        }

                        String partner_name = "";
                        if (!data[5].toString().equals("")) {
                            partner_name = data[5].toString();
                        } else if (data[5] == null) {
                            partner_name = "不需要校验";
                        } else {
                            partner_name = "不需要校验";
                        }
                        logger.info("partner_name: {}", partner_name);
                        //校验数据的合法性
                        Future<JsonObject> checkPartnerNameFuture = checkPartnerNameFuture(conn, count, partner_name);
                        futures.add(checkPartnerNameFuture);

                        String contact_phone = "";
                        if (!data[6].toString().equals(""))
                            contact_phone = data[6].toString();
                        else {
                            errMsgList.add("第" + count + "行的联系电话为空");
                            item_not_null_valid = false;
                        }

                        String gender_name = "";
                        if (!data[7].toString().equals("")) {
                            gender_name = data[7].toString();
                            //校验数据的合法性
                            Future<JsonObject> checkGenderNameFuture = checkGenderNameFuture(conn, count, gender_name);
                            futures.add(checkGenderNameFuture);
                        } else {
                            errMsgList.add("第" + count + "行的性别为空");
                            item_not_null_valid = false;
                        }

                        String age_group_name = "";
                        if (!data[8].toString().equals("")) {
                            age_group_name = data[8].toString();
                            //校验年龄组的合法性
                            String get_code_sql = "select distinct age_group_code from  age_group where age_group_name = $1";
                            Future<JsonObject> checkAgeGroupNameFuture = this.commonRepository.getCodeByNameFuture(conn, get_code_sql, age_group_name, "年龄组名", count, "supplier_info_check_age_group_name");
                            futures.add(checkAgeGroupNameFuture);
                        } else {
                            errMsgList.add("第" + count + "行的年龄组为空");
                            item_not_null_valid = false;
                        }

                        String operation_period_name = "";
                        if (!data[9].toString().equals("")) {
                            operation_period_name = data[9].toString();
                            //校验从业年限的合法性
                            String get_code_sql = "select distinct operation_period_code from  operation_period where operation_period_name = $1";
                            Future<JsonObject> checkOperationPeriodNameFuture = this.commonRepository.getCodeByNameFuture(conn, get_code_sql, operation_period_name, "从业年限名", count, "supplier_info_check_operation_period_name");
                            futures.add(checkOperationPeriodNameFuture);
                        } else {
                            errMsgList.add("第" + count + "行的从业年限为空");
                            item_not_null_valid = false;
                        }

                        //可以选填
                        String processe_site_address = data[10].toString();

                        double processe_site_area;
                        if (!data[11].toString().equals(""))
                            processe_site_area = Double.parseDouble(data[11].toString());
                        else {
                            processe_site_area = 0;
                        }

                        double cold_storage_capacity;
                        if (!data[12].toString().equals(""))
                            cold_storage_capacity = Double.parseDouble(data[12].toString());
                        else {
                            cold_storage_capacity = 0;
                        }

                        double contracted_plant_area;
                        if (!data[13].toString().equals(""))
                            contracted_plant_area = Double.parseDouble(data[13].toString());
                        else {
                            contracted_plant_area = 0;
                        }
                        //通过非空校验,将该行数据暂存入supplier_info_batch
                        if (item_not_null_valid) {
                            supplier_info_batch.add(Tuple.of(supplier_name, site_name, contact_phone, processe_site_address, processe_site_area, cold_storage_capacity, contracted_plant_area));
                        } else {
                            //若某一行数据非空检测结果不合法
                            responseJSObj.put("msg", responseJSObj.getString("msg") + errMsgList.toString());
                            //跳出循环
                            break;
                        }
                    } //end if (line.trim() != "") {  //读取一行数据结束
                } //end while((line = bReader.readLine()) != null){  //所有数据读取完毕
                //数据读取完毕后，关闭bReader
                if (bReader != null) {
                    bReader.close();
                }
                // 关闭OPCPackage
                p.close();

                /***************  读取数据完毕，通过数据非空校验 ******/
                if (item_not_null_valid) {
                    logger.info("通过非空校验,继续执行值校验, future个数: {}", futures.size());
                    //继续执行数据合法校验
                    CompositeFuture.join(futures).onComplete(ar2 -> {
                        if (ar2.succeeded()) {
                            JsonObject check_result = new JsonObject();
                            boolean check_value_valid = true;
                            String exist_supplier_code = ar2.result().list().get(0).toString();
                            int[] supplier_codes = new int[1];
                            int new_supplier_code = 100000;
                            // 为new_supplier_code赋值
                            if (!exist_supplier_code.equals("")) {
                                new_supplier_code = Integer.valueOf(exist_supplier_code) + 1;
                            } else {
                                new_supplier_code = 100000;
                            }
                            supplier_codes[0] = new_supplier_code;
                            //需要知道每一个future对应的第几行第几列，从而准确取出相应的字段的编码
                            int tmp_row_num = 0;
                            String[] row_result = new String[9];
                            String method = "insert";
                            List<Tuple> insert_data_batch = new ArrayList<>();
                            List<Tuple> update_data_batch = new ArrayList<>();
                            for (int i = 1; i < futures.size(); i++) {
                                check_result = (JsonObject) ar2.result().list().get(i);
                                //确定行数,从0开始 i = 1-9: row_num=0;  i = 10-18: row_num=1;
                                int row_num = (i - 1) / 9;
                                //确定列数，从0开始
                                int col_num = (i - 1) % 9;
                                if (row_num <= tmp_row_num) {
                                    if (check_result.getInteger("code") == -1) { //没有通过值校验
                                        check_value_valid = false;
                                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; " + check_result.getString("msg"));
                                    } else {
                                        //通过校验,取出code存放在数组里
                                        row_result[col_num] = check_result.getString("data");
                                        //针对第4列供应商名，判断是更新还是新增数据的操作
                                        if (col_num == 3 && !check_result.getString("data").equals(""))
                                            method = "update";
                                    }
                                    //当前项为一行的最后一列
                                    if (i == (tmp_row_num + 1) * 9) {
                                        // 处理当前一行数据;还原为一行一行数据进行处理
                                        Tuple tempTuple = supplier_info_batch.get(tmp_row_num);
                                        //处理特殊字段销售地编码:
                                        String site_code = row_result[4];
                                        String firstChar = site_code.substring(0, 1);
                                        if (firstChar.equals("["))
                                            site_code = site_code.substring(1, site_code.length() - 1);
                                        site_code = site_code.replace(", ", ",");
                                        if (method.equals("insert")) {
                                            insert_data_batch.add(Tuple.of(row_result[0], row_result[1], row_result[2], String.valueOf(supplier_codes[0]), tempTuple.getString(0), site_code, tempTuple.getString(1),
                                            row_result[5], tempTuple.getString(2), Integer.valueOf(row_result[6]), row_result[7], row_result[8], tempTuple.getString(3), tempTuple.getDouble(4), tempTuple.getDouble(5), tempTuple.getDouble(6)));

                                            supplier_codes[0] = supplier_codes[0] + 1;
                                        } else {
                                            update_data_batch.add(Tuple.of(row_result[0], row_result[1], row_result[2], site_code, tempTuple.getString(1), row_result[5], tempTuple.getString(2),
                                            Integer.valueOf(row_result[6]), row_result[7], row_result[8], tempTuple.getString(3), tempTuple.getDouble(4), tempTuple.getDouble(5), tempTuple.getDouble(6), tempTuple.getString(0)));
                                        }
                                        //更新method的值为新增
                                        method = "insert";
                                        tmp_row_num += 1;
                                    }
                                } else {
                                    tmp_row_num = row_num;
                                    row_result = new String[9];
                                    //检查新的一行第一列的校验结果并存入row_result里
                                    if (check_result.getInteger("code") == -1) { //没有通过值校验
                                        check_value_valid = false;
                                        responseJSObj.put("msg", responseJSObj.getString("msg") + "; " + check_result.getString("msg"));
                                    } else { //通过校验
                                        row_result[col_num] = check_result.getString("data");
                                    }
                                }
                            }

                            /********** 通过全部数据合法性校验，准备进行写入或更新 *****/
                            if (check_value_valid) {
                                logger.info("通过全部值校验，准备进行写入或更新");
                                @SuppressWarnings("rawtypes")
                                List<Future> futures2 = new ArrayList<Future>();
                                //所有数据通过校验,执行insert or update 写入或更新数据
                                if (insert_data_batch.size() > 0) {
//	                    			Future<JsonObject> insertSupplierInfoBatchFuture = insertSupplierInfoBatchFuture(conn, insert_data_batch);
                                    String insert_sql = "INSERT INTO supplier_info(goods_gather_area_code, class_lv1_code, grade_code, supplier_code, " +
                                    "supplier_name, site_code, site_name, partner_code, contact_phone, gender_type, age_group_code, " +
                                    "operation_period_code, processe_site_address, processe_site_area, cold_storage_capacity, " +
                                    "contracted_plant_area ) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15,  $16 ) RETURNING id";
                                    Future<RowSet<Row>> insertSupplierInfoBatchFuture = this.commonRepository.insertOrUpdateBatch(conn, insert_sql, insert_data_batch, "insert_supplier_info_batch");
                                    futures2.add(insertSupplierInfoBatchFuture);
                                    logger.info("insert_data_batch size: {}", insert_data_batch.size());
                                }
                                if (update_data_batch.size() > 0) {
                                    //Future<JsonObject> updateSupplierInfoBatchFuture = updateSupplierInfoBatchFuture(conn, update_data_batch);
                                    String update_sql = "update supplier_info set goods_gather_area_code = $1, class_lv1_code = $2,  grade_code = $3, " +
                                    "site_code = $4, site_name = $5, partner_code = $6, contact_phone = $7, gender_type = $8, age_group_code = $9, " +
                                    "operation_period_code = $10, processe_site_address = $11, processe_site_area = $12, cold_storage_capacity = $13," +
                                    " contracted_plant_area = $14  where supplier_name = $15 RETURNING id";
                                    Future<RowSet<Row>> updateSupplierInfoBatchFuture = this.commonRepository.insertOrUpdateBatch(conn, update_sql, update_data_batch, "update_supplier_info_batch");
                                    futures2.add(updateSupplierInfoBatchFuture);
                                    logger.info("update_data_batch size: {}", update_data_batch.size());
                                }
                                if (futures2.size() > 0) {
                                    logger.info("futures2.size(): {}", futures2.size());
                                    CompositeFuture.join(futures2).onComplete(ar4 -> {
                                        if (ar4.succeeded()) {
                                            JsonObject insert_or_update_result = new JsonObject();
                                            for (int j = 0; j < futures2.size(); j++) {
                                                insert_or_update_result = (JsonObject) ar4.result().list().get(j);
                                                logger.info("insert_or_update_result: {}", insert_or_update_result.getString("msg"));
                                                responseJSObj.put("msg", responseJSObj.getString("msg") + "; " + insert_or_update_result.getString("msg"));
                                            }
                                        } else {
                                            responseJSObj.put("msg", responseJSObj.getString("msg") + "; " + ar4.result().cause().getMessage());
                                        }

                                        //继续删除文件
                                        vertx.fileSystem().delete(file_name, res0 -> {
                                            if (res0.succeeded()) {
                                                logger.info("读取excel后删除文件 {} 成功", file_name);
                                                responseJSObj.put("code", 200).put("msg", responseJSObj.getString("msg") + "; 删除文件成功; 处理供应商信息完毕");
                                            } else {
                                                logger.info("读取excel后删除文件 {} 失败 ", file_name);
                                                responseJSObj.put("code", 200).put("msg", responseJSObj.getString("msg") + "; 删除文件失败; 处理供应商信息完毕");
                                            }
                                            promise.complete(responseJSObj);
                                        });
                                    });
                                } else {
                                    //继续删除文件
                                    vertx.fileSystem().delete(file_name, res0 -> {
                                        if (res0.succeeded()) {
                                            logger.info("读取excel后删除文件 {} 成功", file_name);
                                            responseJSObj.put("code", 200).put("msg", responseJSObj.getString("msg") + "; 删除文件成功; 处理供应商信息完毕");
                                        } else {
                                            logger.info("读取excel后删除文件 {} 失败 ", file_name);
                                            responseJSObj.put("code", 200).put("msg", responseJSObj.getString("msg") + "; 删除文件失败; 处理供应商信息完毕");
                                        }
                                        promise.complete(responseJSObj);
                                    });
                                }
                            } else {
                                //部分数据校验没有通过，全部不写入数据；返回提示信息
                                //继续删除文件
                                vertx.fileSystem().delete(file_name, res0 -> {
                                    if (res0.succeeded()) {
                                        logger.info("读取excel后删除文件 {} 成功", file_name);
                                        responseJSObj.put("code", 200).put("msg", responseJSObj.getString("msg") + "; 删除文件成功; 处理供应商信息完毕");
                                    } else {
                                        logger.info("读取excel后删除文件 {} 失败 ", file_name);
                                        responseJSObj.put("code", 200).put("msg", responseJSObj.getString("msg") + "; 删除文件失败; 处理供应商信息完毕");
                                    }
                                    promise.complete(responseJSObj);
                                });
                            }

                        } else {
                            promise.fail(CommonUtils.createExceptionJsonObjectStr(ar2, "batchImportSupplierInfo", "执行数据合法性校验发生错误"));
                        }
                    });
                } else { //没有通过数据字段非空校验
                    responseJSObj.put("code", -1).put("msg", "没有通过非空校验");
                    promise.complete(responseJSObj);
                }
            } catch (Exception err) {  //end try ( OPCPackage p = OPCPackage.open(file_name, PackageAccess.READ)) {
                promise.fail(CommonUtils.createExceptionJsonObjectStr(err, "handleExcelFile in batchImportSupplierInfo", "打开excel文件报错"));
            }
        }).onFailure(err -> promise.fail(CommonUtils.createExceptionJsonObjectStr(err, "batchImportSupplierInfo", "数据库连接失败")));
        return promise.future();
    }

    /**
     * 校验销售地区，比较复杂，需要对多个地区名进行校验，最后组装地区编码
     *
     * @param conn
     * @param row_num
     * @param site_name
     * @return
     */
    public Future<JsonObject> checkSiteNameFuture(SqlConnection conn, int row_num, String site_name) {
        Promise<JsonObject> promise = Promise.promise();
        JsonObject resultJSObj = new JsonObject();
        resultJSObj.put("msg", "销售地校验: ");

        //第二层级的多个 futures
        @SuppressWarnings("rawtypes")
        List<Future> futures = new ArrayList<Future>();
        logger.info(site_name);
        String[] site_names = site_name.split(",");
        logger.info("site1: " + site_names[0]);
        List<String> site_name_list = Arrays.asList(site_names);
        logger.info("site_name_list: " + site_name_list);

        site_name_list.forEach(site_name_item -> {
            Future<JsonObject> checkSingleSiteNameFuture = checkSingleSiteNameFuture(conn, site_name_item);
            futures.add(checkSingleSiteNameFuture);
        });
        CompositeFuture.join(futures).onComplete(ar2 -> {
            if (ar2.succeeded()) {
                JsonObject check_result = new JsonObject();
                List<String> site_codes = new ArrayList<String>();
                boolean result_valid = true;
                for (int i = 0; i < futures.size(); i++) {
                    check_result = (JsonObject) ar2.result().list().get(i);
                    if (check_result.getInteger("code") == -1) {
                        result_valid = false;
                    } else {
                        site_codes.add(check_result.getString("data"));
                    }
                }
                if (result_valid) {
                    logger.info("site_codes: {}", site_codes.toString());
                    resultJSObj.put("code", 0).put("data", site_codes.toString());
                } else {
                    resultJSObj.put("code", -1).put("data", "").put("msg", "第" + row_num + "行的销售地: " + site_name + "不合法");
                }
            } else {
                resultJSObj.put("code", -1).put("data", "").put("msg", "第" + row_num + "行的销售地: " + site_name + "不合法");
            }
            promise.complete(resultJSObj);
        });

        return promise.future();
    }

    public Future<JsonObject> checkSingleSiteNameFuture(SqlConnection conn, String site_name) {
        Promise<JsonObject> promise = Promise.promise();
        JsonObject resultJSObj = new JsonObject();
        String sql = "select distinct site_code from site where  site_name = '" + site_name + "'";
        this.commonRepository.commonSelectRowsetBySql(conn, sql)
        .onSuccess(rows -> {
            if (rows.rowCount() > 0) {
                String[] temp_value = new String[1];
                rows.forEach(item -> {
                    temp_value[0] = item.getString("site_code");
                });
                resultJSObj.put("code", 0).put("data", temp_value[0]);
            } else {
                resultJSObj.put("code", -1).put("data", "");
            }
            promise.complete(resultJSObj);
        })
        .onFailure(throwable -> {
            logger.info("in checkClassLv1NameFuture: " + throwable.toString());
            resultJSObj.put("code", -1).put("msg", throwable.toString());
            promise.complete(resultJSObj);
        });
        return promise.future();
    }

    /**
     * 校验供应链合伙人
     *
     * @param conn
     * @param row_num
     * @param partner_name
     * @return
     */
    public Future<JsonObject> checkPartnerNameFuture(SqlConnection conn, int row_num, String partner_name) {
        Promise<JsonObject> promise = Promise.promise();
        JsonObject resultJSObj = new JsonObject();
        if (partner_name.equals("不需要校验")) {
            resultJSObj.put("code", 0).put("data", "").put("msg", "没有选择供应链合伙人");
            promise.complete(resultJSObj);
        } else {
            String sql = "select distinct partner_code from  supply_chain_partner where partner_name = '" + partner_name + "'";
            this.commonRepository.commonSelectRowsetBySql(conn, sql)
            .onSuccess(rows -> {
                if (rows.rowCount() > 0) {
                    String[] temp_value = new String[1];
                    rows.forEach(item -> {
                        temp_value[0] = item.getString("partner_code");
                    });
                    resultJSObj.put("code", 0).put("data", temp_value[0]);
                } else {
                    resultJSObj.put("code", -1).put("msg", "第" + row_num + "行的捆绑供应链合伙人名: " + partner_name + "不合法");
                }
                promise.complete(resultJSObj);
            })
            .onFailure(throwable -> {
                logger.info("in checkPartnerNameFuture: " + throwable.toString());
                resultJSObj.put("code", -1).put("msg", throwable.toString());
                promise.complete(resultJSObj);
            });
        }
        return promise.future();
    }

    /**
     * 校验性别
     *
     * @param conn
     * @param row_num
     * @param gender_name
     * @return
     */
    public Future<JsonObject> checkGenderNameFuture(SqlConnection conn, int row_num, String gender_name) {
        Promise<JsonObject> promise = Promise.promise();
        JsonObject resultJSObj = new JsonObject();
        if (gender_name.equals("男")) {
            resultJSObj.put("code", 0).put("data", "1");
        } else if (gender_name.equals("女")) {
            resultJSObj.put("code", 0).put("data", "0");
        } else {
            resultJSObj.put("code", -1).put("msg", "第" + row_num + "行的性别: " + gender_name + "不合法");
        }
        promise.complete(resultJSObj);
        return promise.future();
    }


    //暂时废弃
    public Future<JsonObject> insertSupplierInfoBatchFuture(SqlConnection conn, List<Tuple> insert_data_batch) {
        Promise<JsonObject> promise = Promise.promise();
        JsonObject responseJSObj = new JsonObject();
        String insert_sql = "INSERT INTO supplier_info(goods_gather_area_code, class_lv1_code, grade_code, supplier_code, " +
        "supplier_name, site_code, site_name, partner_code, contact_phone, gender_type, age_group_code, " +
        "operation_period_code, processe_site_address, processe_site_area, cold_storage_capacity, " +
        "contracted_plant_area ) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15,  $16 ) RETURNING id";
        this.commonRepository.insertOrUpdateBatch(conn, insert_sql, insert_data_batch, "insert_supplier_info_batch")
        .onComplete(ar4 -> {
            if (ar4.succeeded()) {
                responseJSObj.put("code", 200);
                responseJSObj.put("msg", "新增供应商基本信息信息成功");
                logger.info("新增供应商基本信息信息成功");

            } else {
                responseJSObj.put("code", -1);
                responseJSObj.put("msg", "新增供应商基本信息信息,异常信息: " + ar4.cause().getMessage());
            }
            promise.complete(responseJSObj);
        });
        return promise.future();
    }

    //暂时废弃
    public Future<JsonObject> updateSupplierInfoBatchFuture(SqlConnection conn, List<Tuple> update_data_batch) {
        Promise<JsonObject> promise = Promise.promise();
        JsonObject responseJSObj = new JsonObject();
        String update_sql = "update supplier_info set goods_gather_area_code = $1, class_lv1_code = $2,  grade_code = $3, " +
        "site_code = $4, site_name = $5, partner_code = $6, contact_phone = $7, gender_type = $8, age_group_code = $9, " +
        "operation_period_code = $10, processe_site_address = $11, processe_site_area = $12, cold_storage_capacity = $13," +
        " contracted_plant_area = $14  where supplier_name = $15 RETURNING id";
        this.commonRepository.insertOrUpdateBatch(conn, update_sql, update_data_batch, "update_supplier_info_batch")
        .onComplete(ar4 -> {
            if (ar4.succeeded()) {
                responseJSObj.put("code", 200);
                responseJSObj.put("msg", "更新供应商基本信息信息成功");
                logger.info("更新供应商基本信息信息成功");

            } else {
                responseJSObj.put("code", -1);
                responseJSObj.put("msg", "更新供应商基本信息信息,异常信息: " + ar4.cause().getMessage());
            }
            promise.complete(responseJSObj);
        });
        return promise.future();
    }

}
